﻿using MockPrjTeam1.Biz;
using MockPrjTeam1.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MockPrjTeam1.Pages.Views
{
    public partial class GovOfficeDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var id = Convert.ToString(Request.QueryString["id"]);
                if (!string.IsNullOrEmpty(id))
                {
                    var biz = new GovOfficeBiz();
                    var gov = biz.GetOne(Convert.ToInt32(id));
                    txtGovOfficeName.Text = gov.Name;
                    txtDescription.Text = gov.Description;
                    txtCounty.Text = gov.County;
                    if (gov.Active)
                        cbActive.Checked = true;
                    else
                        cbActive.Checked = false;
                }
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            var biz = new GovOfficeBiz();

            var id1 = Convert.ToString(Request.QueryString["id"]);
            if (!string.IsNullOrEmpty(txtGovOfficeName.Text))
            {
                if (!string.IsNullOrEmpty(id1))
                {
                    if (biz.CheckName(txtGovOfficeName.Text, Convert.ToInt32(id1)))
                    {
                        var entity = new GovOffice();
                        entity.ID = Convert.ToInt32(id1);
                        entity.Name = txtGovOfficeName.Text;
                        entity.Description = txtDescription.Text;
                        if (cbActive.Checked)
                            entity.Active = true;
                        else
                            entity.Active = false;
                        if (biz.Edit(entity))
                            Response.Redirect("../Views/GovOfficeList.aspx");
                    }
                    else
                        lbMessage.Text = "Existed Name";
                }

                else
                {
                    if (biz.CheckName(txtGovOfficeName.Text, 0))
                    {
                        var entity = new GovOffice();
                        entity.Name = txtGovOfficeName.Text;
                        entity.Description = txtDescription.Text;
                        if (cbActive.Checked)
                            entity.Active = true;
                        else
                            entity.Active = false;
                        if (biz.Create(entity))
                            Response.Redirect("../Views/GovOfficeList.aspx");
                    }
                    else
                        lbMessage.Text = "Existed Name";
                }
            }
            else
                lbMessage.Text = "Name is not null!";
        }
    }
}